<?php

/**
 * This is the model class for table "tbproveedor".
 *
 * The followings are the available columns in table 'tbproveedor':
 * @property string $Pro_idproveedor_PK
 * @property string $Pro_nombre
 * @property string $Pro_telefono
 * @property string $Pro_fax
 * @property string $Pro_direccion
 * @property string $Pro_cedula
 *
 * The followings are the available model relations:
 * @property Tbproducto[] $tbproductos
 */
class proveedor extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbproveedor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Pro_idproveedor_PK, Pro_nombre, Pro_telefono, Pro_direccion, Pro_cedula', 'required'),
			array('Pro_idproveedor_PK', 'length', 'max'=>30),
			array('Pro_nombre', 'length', 'max'=>25),
			array('Pro_telefono, Pro_fax, Pro_cedula', 'length', 'max'=>15),
			array('Pro_direccion', 'length', 'max'=>100),
			//Validacion agregada el dia 06/02/2014. hacer un campo unico.
			array('Pro_idproveedor_PK','unique','attributeName'=>'Pro_idproveedor_PK',"message"=>"Código de proveedor ya existe..."),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('Pro_idproveedor_PK, Pro_nombre, Pro_telefono, Pro_fax, Pro_direccion, Pro_cedula', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbproductos' => array(self::HAS_MANY, 'Tbproducto', 'Pro_idproveedor_FK'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Pro_idproveedor_PK' => 'Código',
			'Pro_nombre' => 'Nombre',
			'Pro_telefono' => 'Teléfono',
			'Pro_fax' => 'Fax',
			'Pro_direccion' => 'Dirección',
			'Pro_cedula' => 'Cédula',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Pro_idproveedor_PK',$this->Pro_idproveedor_PK,true);
		$criteria->compare('Pro_nombre',$this->Pro_nombre,true);
		$criteria->compare('Pro_telefono',$this->Pro_telefono,true);
		$criteria->compare('Pro_fax',$this->Pro_fax,true);
		$criteria->compare('Pro_direccion',$this->Pro_direccion,true);
		$criteria->compare('Pro_cedula',$this->Pro_cedula,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination' => array(  'pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']))
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return proveedor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
